////////////////////////////////////////////////////
/// This file is part of Marstone Code Studio    ///
///    Copyright(c)2008 - 2008, Marstone Studio  ///
///           Author: marstone                   ///
///       Email: marstone@marstone.net           ///
///           Since: 2006, Haier FRACAS          ///
////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Drawing;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace Studio.Network.Utilities
{

    public class SendMail
    {

        /// <summary>
        /// Send Email by from, to, subject & body
        /// </summary>
        protected bool SendEmail(string from, string to, string subject, string body, string host, int port, string password)
        {
            try
            {
                // New an Email
                MailMessage Email = new MailMessage(from, to, subject, body);
                Email.BodyEncoding = System.Text.Encoding.UTF8;
                // New a SmtpClient
                // SmtpClient Client = new SmtpClient();
                SmtpClient Client = new SmtpClient(host, port);
                string frompass = password;
                // Credential
                if (false == string.IsNullOrEmpty(frompass))
                {
                    Client.Credentials = new NetworkCredential(from, frompass);
                }
                // Send the Email & Block
                Client.Send(Email);
                return true;
            }
            catch (Exception ex)
            {

                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                return false;
            }
        }

        public static bool ValidateEmail(string email)
        {
            string emailPattern = "^[\\w|-|\\.]+@([a-zA-Z_0-9]+?\\.)+[a-zA-Z]{2,4}$";
            Regex regexEmail = new Regex(emailPattern);
            return regexEmail.IsMatch(email);
        }

    }
        
}
